package FP 
{
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Ty Kimmey
	 */
	public class ObjectManager extends MovieClip
	{
		private var aGame:Game;
		private var levelXML:XML;
		
		private var allWalls:Array = new Array();
		public var activeWalls:Array = new Array();
		public var pooledWalls:Array = new Array();
		
		private var allNerds:Array = new Array();
		public var activeNerds:Array = new Array();
		public var pooledNerds:Array = new Array();
		
		private var allBabies:Array = new Array();
		public var activeBabies:Array = new Array();
		public var pooledBabies:Array = new Array();
		
		private var walls:int = 0;
		private var nerds:int = 0;
		public var babies:int = 0;
		
		public function ObjectManager(game:Game, lvlXML:XML, currentLvl:int) 
		{
			aGame = game;
			levelXML = lvlXML;
			walls = levelXML.levels.level[currentLvl].objects[0].walls;
			nerds = levelXML.levels.level[currentLvl].objects[0].nerds;
			babies = levelXML.levels.level[currentLvl].objects[0].babies;
			
			for (var i:int = 0; i < walls; i++)
			{
				allWalls.push(new Wall(game));
			}
			
			for (var n:int = 0;  n < nerds; n++)
			{
				allNerds.push(new Nerd(game));
			}
			
			for (var b:int = 0; b < babies; b++)
			{
				allBabies.push(new Baby(game));
			}
			pooledWalls = allWalls.concat();
			pooledNerds = allNerds.concat();
			pooledBabies = allBabies.concat();
		}
		
		//public function get ActiveWalls():Array { return activeWalls; }
		//public function get ActiveNerds():Array { return activeNerds; }
		//public function get ActiveBabies():Array { return activeBabies; }
		//public function get PooledNerds():Array { return pooledNerds; }
		
		public function spawnObject()
		{
			var mathX = (Math.random() * 6)
			mathX = Math.floor(mathX);
			trace(mathX);
			
			//if 1 or 2 spawn wall
			if (mathX == 0 || mathX == 1)
			{	//if pooled wall != null
				if (pooledWalls.length != 0)
				{	//spawn wall
					var w:Wall = pooledWalls.pop();
					aGame.spawnWalls(w);					
				}
			}
			if (mathX == 2 || mathX == 3)
			{
				if (pooledNerds.length != 0)
				{
					var n:Nerd = pooledNerds.pop();
					aGame.spawnNerds(n);
				}
			}
			if (mathX == 4 || mathX == 5)
			{
				if (pooledBabies.length != 0)
				{
					var b:Baby = pooledBabies.pop();
					aGame.spawnBabies(b);
				}
			}
			
		}
		
		public function updateObject()
		{
			var w:Wall;
			var n:Nerd;
			var b:Baby;
			
			for (var i:int = 0; i < activeWalls.length; i++)
			{
				w = (activeWalls[i] as Wall);
				w.objectMove();
				
				if (w.y > w.stage.stageHeight)
				{
					activeWalls.splice(i, 1);
					i--;
				}
			}
			
			for (var s:int = 0; s < activeNerds.length; s++)
			{
				n = (activeNerds[i] as Nerd);
				n.objectMove();
				
				if (n.y > n.stage.stageHeight)
				{
					activeNerds.splice(s, 1);
					s--;
				}
				
			}
			
			for (var c:int = 0; c < activeBabies.length; c++)
			{
				b = (activeBabies[c] as Baby);
				b.objectMove();
				
				if (b.y > b.stage.stageHeight)
				{
					activeBabies.splice(c, 1);
					c--;
				}
			}
		}
		
	}

}